<template>
  <div>
    <el-dialog
      title="添加员工"
      width="480px"
      :close-on-click-modal="false"
      :visible="dialogVisible"
      :before-close="handleClose"
      @open="openDialog"
    >
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-width="80px">
          <el-form-item label="员工姓名" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="登录账号" prop="userName">
            <el-input v-model="addForm.userName" />
          </el-form-item>
          <el-form-item label="联系方式" prop="phonenumber">
            <el-input v-model="addForm.phonenumber" />
          </el-form-item>
          <el-form-item label="分配角色" prop="roleId">
            <el-select v-model="addForm.roleId" placeholder="请选择角色">
              <el-option
                v-for="item in roleList"
                :key="item.roleId"
                :label="item.roleName"
                :value="item.roleId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="员工状态" prop="status">
            <el-radio-group v-model="addForm.status">
              <el-radio :label="0">禁用</el-radio>
              <el-radio :label="1">启用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="handleClose">取 消</el-button>
        <el-button size="mini" type="primary" @click="addemployee">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getsysroleAPI } from '@/api/sysrole'
import { addemployeeAPI, viewemployeeAPI, editemployeeAPI } from '@/api/employee'
export default {
  name: 'Addemployee',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // dialogVisible: true,
      roleList: [],
      addForm: {
        name: '',
        phonenumber: '',
        roleId: '',
        status: 1,
        userName: ''
      },
      addFormRules: {
        name: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '请输入登录账号', trigger: 'blur' }
        ],
        phonenumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' },
          { pattern: /^(?:(?:\+|00)86)?1\d{10}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        roleId: [
          { required: true, message: '请分配角色', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择员工状态', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // this.getsysrole()
  },
  methods: {
    // 修改父组件的dialogVisible值
    handleClose() {
      this.$emit('update:dialogVisible', false)
      this.$refs.addForm.resetFields()
      delete this.addForm.id
    },
    // 查询角色接口
    async openDialog() {
      const res = await getsysroleAPI()
      // console.log(res)
      this.roleList = res.data
    },
    // 添加员工，编辑员工
    async addemployee() {
      this.$refs.addForm.validate(async flag => {
        if (!flag) return
        if (this.addForm.id) {
          delete this.addForm.createTime
          await editemployeeAPI(this.addForm)
          this.$message.success('编辑成功')
          this.handleClose()
          this.$emit('getemployeelist')
        } else {
          await addemployeeAPI(this.addForm)
          this.$message.success('添加员工成功')
          this.$emit('getemployeelist')
          this.handleClose()
        }
      })
    },
    // 回显员工
    async viewemployee(id) {
      // console.log(id)
      const res = await viewemployeeAPI(id)
      // console.log(res.data)
      this.addForm = res.data
    }
  }
}
</script>

<style lang="scss" scoped>
.form-container{
  padding:0px 80px;
}
</style>
